package com.etc.controller;

import com.aliyuncs.exceptions.ClientException;
import com.etc.pojo.user.User;
import com.etc.service.UserService;
import com.etc.utils.MD5Utils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;



/**
 * @author Administrator
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    //删除用户
    @RequestMapping(value = "/deletebyid")
    public void deleteById(PrintWriter out ,String id){
        Integer delete = userService.deleteById(Integer.parseInt(id));
        if (delete == 1){
            out.print(1);
        }else {
            out.print(2);
        }
    }

    @RequestMapping(value = "/cookie",method = RequestMethod.POST)
    public ModelAndView hello(User user, HttpServletRequest request, HttpServletResponse response) throws IOException {
        ModelAndView modelAndView = new ModelAndView();
        PrintWriter writer = response.getWriter();
        try{
            User byUsername = userService.findByUsername(user);

            if(byUsername != null){
                HttpSession session = request.getSession();
                session.setAttribute("user",byUsername);
                modelAndView.setViewName("shucai");
            }else{
                modelAndView.setViewName("login");
            }
        }catch (Exception e){

            modelAndView.setViewName("login");
        }

        writer.flush();
        return modelAndView;
    }
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public void insert(PrintWriter out ,
                       @RequestParam(value = "username") String username,
                       @RequestParam(value = "password") String password){
        Integer integer = userService.insertUser(new User(username, MD5Utils.getPwd(password)));
        if (integer == 1){
            out.print(1);
        }else {
            out.print(2);
        }
    }

    /**
     *
     * @param request
     * @param pageno 页数
     * @param pagesize 显示页数
     * @return
     */
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public String user(HttpServletRequest request,
                      @RequestParam(value = "pageno",required=true,defaultValue = "1") Integer pageno,
                       @RequestParam(value = "pagesize",required=true,defaultValue = "3")  Integer pagesize){

        PageInfo<User> byPage = userService.findByPage(pageno, pagesize);
        request.setAttribute("userlist",byPage);
        return "user";
    }


    @RequestMapping(value = "add",method = RequestMethod.GET)
    public String add(){
        return "insert";
    }

    @RequestMapping(value = "shouji",method = RequestMethod.GET)
    public String shouji(){
        return "shouji";
    }
    @RequestMapping(value = "yanzhengma" , method = RequestMethod.POST)
    public void yanzhengma(String phone,PrintWriter out) throws ClientException {
        String v = userService.messageAcquisition(phone);

       if(v.equals(1)){
           out.print(1);
       } else{
           out.print(2);
       }
    }


    /**
     *
     * @param phoneNumbers  手机号
     * @param yanzheng      验证码
     * @param out
     * 1 手机号不存在或者丢失
     * 2 验证码不存在或者失效
     * 3 redis验证码不存在或者失效
     * 4 验证码匹配成功
     * 5 验证码匹配失败
     */
    @RequestMapping(value = "yanzheng",method = RequestMethod.POST)
    public void yanzheng(String phoneNumbers, String yanzheng,PrintWriter out){
       out.print(userService.yanzheng(phoneNumbers,yanzheng));

    }
}
